package com.tico.editor.editors.controller;

import com.alibaba.fastjson.JSONObject;
import com.tico.common.pojo.Result;
import com.tico.common.pojo.ResultHelper;
import com.tico.common.utils.BaseQuery;
import com.tico.common.utils.JsonUtils;
import com.tico.common.utils.QueryResult;
import com.tico.editor.editors.domain.HttpEditor;
import com.tico.editor.editors.domain.KafkaTopic;
import com.tico.editor.editors.domain.PubilcData;
import com.tico.editor.editors.param.HttpEditorParam;
import com.tico.editor.editors.param.PubilcDataParam;
import com.tico.editor.editors.service.HttpEditorService;
import com.tico.editor.editors.service.PubilcDataService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.UUID;

@RestController
@RequestMapping("/public")
@Slf4j
public class PublicDataController {
    @Autowired
    private PubilcDataService pubilcDataService;

    @GetMapping(value = "/list")
    public JSONObject list(PubilcDataParam pubilcDataParam) {
        Result result = new Result();
        try {
            BaseQuery baseQuery = new BaseQuery();
            baseQuery.setPage(pubilcDataParam.getPage());
            baseQuery.setLimit(pubilcDataParam.getLimit());
            QueryResult<PubilcData> pageInfo = pubilcDataService.findPageInfo(baseQuery);
            result.addModel("count",pageInfo.getTotalRecord());
            result.addModel("data", JsonUtils.toUnderlineJSONFilterString(PubilcData.class,pageInfo.getResultList(),new String[]{}) );
            result.setMessage("成功");
            result.setSuccess(true);
        }catch (Exception e){
            result.setMessage("失败");
            result.setSuccess(false);
        }
        return ResultHelper.renderAsJsonWipeData(result);
    }
    @PostMapping(value = "/save")
    public JSONObject save(@RequestBody PubilcData pubilcData) {
        Result res = new Result();
        try {
            pubilcData.setId(UUID.randomUUID().toString().replaceAll("-", ""));
            pubilcDataService.insert(pubilcData);
            res.setMessage("成功");
            res.setSuccess(true);
        }catch (Exception e){
            res.setMessage("失败");
            res.setSuccess(false);
        }
        return ResultHelper.renderAsJson(res);
    }
    @PostMapping(value = "/test")
    public JSONObject test(@RequestBody PubilcData pubilcData) {
        Result res = new Result();
        try {
            pubilcData.setId(UUID.randomUUID().toString().replaceAll("-", ""));
            String s = pubilcDataService.modelHttp(pubilcData.getHttpId(), pubilcData.getPublicKey());
            res.setMessage("成功");
            res.addModel("data",s);
            res.setSuccess(true);
        }catch (Exception e){
            res.setMessage("失败");
            res.setSuccess(false);
        }
        return ResultHelper.renderAsJson(res);
    }

    @PostMapping(value = "/delete")
    public JSONObject delete(@RequestBody PubilcDataParam pubilcDataParam, HttpServletRequest request) {
        Result res = new Result();
        try {
            String str[] = pubilcDataParam.getIds().split(",");
            for(int i=0;i<str.length;i++){
                pubilcDataService.delete(str[i]);
            }
            res.setMessage("成功");
            res.setSuccess(true);
        }catch (Exception e){
            res.setMessage("失败");
            res.setSuccess(false);
        }
        return ResultHelper.renderAsJson(res);
    }
}
